package mall.controller.admin;

import mall.core.jdbc.Page;
import mall.core.web.AbstractAdminController;
import mall.entity.Category;
import mall.entity.Image;
import mall.entity.Product;
import mall.service.CategoryService;
import mall.service.ImageService;
import mall.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@WebServlet("/admin/product")
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
        maxFileSize = 1024 * 1024 * 10,      // 10MB
        maxRequestSize = 1024 * 1024 * 50)// 50MB
public class AdminProductController extends AbstractAdminController {

    private final ProductService productService = new ProductService();
    private final ImageService imageService = new ImageService();
    private final CategoryService categoryService = new CategoryService();


    @Override
    protected Product getParameterForEntity(HttpServletRequest request) {
        Product product = new Product();

        String id = request.getParameter("id");
        if (id != null && !id.isEmpty()) {
            product.setId(Integer.parseInt(id));
        }

        String categoryId = request.getParameter("categoryId");
        if (categoryId != null && !categoryId.isEmpty()) {
            product.setCategoryId(Integer.parseInt(categoryId));
        }

        String name = request.getParameter("name");
        if (name != null && !name.isEmpty()) {
            product.setName(name);
        }

        String price = request.getParameter("price");
        if (price != null && !price.isEmpty()) {
            product.setPrice(new BigDecimal(price));
        }

        String description = request.getParameter("description");
        if (description != null && !description.isEmpty()) {
            product.setDescription(description);
        }

        return product;
    }

    /**
     * 新增
     */
    @Override
    public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Part part = request.getPart("image");
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        imageService.upload(new Image(uuid, part.getInputStream()));

        Product product = getParameterForEntity(request);
        product.setImage(uuid);

        productService.add(product);

        redirect("/admin/product?method=viewList", request, response);
    }

    /**
     * 删除
     */
    @Override
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Product product = getParameterForEntity(request);
        productService.deleteById(product.getId());
        redirect("/admin/product?method=viewList", request, response);
    }

    /**
     * 修改
     */
    @Override
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Product product = getParameterForEntity(request);

        String uuid = null;
        Part part = request.getPart("image");
        if (part != null && part.getSize() > 0 && part.getContentType() != null && part.getContentType().startsWith("image/")) {
            uuid = UUID.randomUUID().toString();
            uuid = uuid.replaceAll("-", "");
            imageService.upload(new Image(uuid, part.getInputStream()));
        }

        product.setImage(uuid);
        productService.update(product);

        redirect("/admin/product?method=viewList", request, response);
    }

    /**
     * 查询
     */
    @Override
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Page<Product> page = getParameterForPage(request);
        Product product = getParameterForEntity(request);

        productService.page(page, product);

        request.setAttribute("id", product.getId());
        request.setAttribute("name", product.getName());

        setPageAttribute(request, page);

        forward("/admin/product/list", request, response);
    }

    /**
     * 访问列表页面
     */
    @Override
    public void viewList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Page<Product> page = getParameterForPage(request);
        Product product = getParameterForEntity(request);

        productService.page(page, product);

        setPageAttribute(request, page);

        forward("/admin/product/list", request, response);
    }

    /**
     * 访问新增页面
     */
    @Override
    public void viewCreate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categoryList = categoryService.list();
        request.setAttribute("categoryList", categoryList);

        forward("/admin/product/create", request, response);
    }

    /**
     * 访问编辑页面
     */
    @Override
    public void viewEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Product product = getParameterForEntity(request);
         product = productService.getById(product.getId());
        request.setAttribute("product", product);

        List<Category> categoryList = categoryService.list();
        request.setAttribute("categoryList", categoryList);
        forward("/admin/product/edit", request, response);
    }
}
